package org.yoao.crm.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yoao.crm.DBManager.DBManager;
import org.yoao.crm.struts.form.MenuForm;

/**
 * @author yxwonline.com
 * @description
 * @date 2010-7-21 下午12:40:38 
 */
public class MenuDAO extends DBManager{
	public static final Logger log = Logger.getLogger(MenuDAO.class);
	
	private static MenuDAO dao = new MenuDAO();
	
	public static MenuDAO getMenuDAO(){
		return dao;
	}
	
	public boolean save(MenuForm menu) throws Exception{
		String sql = "insert into menu(keyName, title, formType, sortNumber, layer, isActive, isFirstItem, " +
					 "url, accessSno, accessName, target, headImage, createTime, memo) values('"+menu.getKeyName()+"', '"+
					 menu.getTitle()+"', '"+menu.getFormType()+"', "+menu.getSortNumber()+", "+menu.getLayer()+
					 ", "+menu.getIsActive()+", "+menu.getIsFirstItem()+", '"+menu.getUrl()+"', '"+menu.getAccessSno()+"','"+
					 menu.getAccessName()+"','"+menu.getTarget()+"','"+menu.getHeadImage()+"','"+menu.getCreateTime()+"','"+menu.getMemo()+"')";
		if(update(sql) > -1) return true;
		else return false;
	}
	
	public boolean modify(MenuForm menu) throws Exception{
		String sql = "update menu set title='"+menu.getTitle()+"', formType='"+menu.getFormType()+"', " +
					 "sortNumber="+menu.getSortNumber()+", layer="+menu.getLayer()+", isActive="+
					 menu.getIsActive()+", isFirstItem="+menu.getIsFirstItem()+", url='"+menu.getUrl()+
					 "', accessSno='"+menu.getAccessSno()+"', accessName='"+menu.getAccessName()+
					 "', target='"+menu.getTarget()+"', headImage='"+menu.getHeadImage()+"', createTime='"+menu.getCreateTime()+
					 "', memo='"+menu.getMemo()+"' where keyName='"+menu.getKeyName()+"'";
		if(update(sql) > -1) return true;
		else return false;
	}
	
	public boolean exist(String keyName){
		String sql = "select keyName from menu where keyName='"+keyName+"'";
		String result = querySingleValue(sql);
		if(result != null) return true;
		else return false;
	}
	
	public MenuForm findMenuByKey(String keyName) throws Exception{
		String sql = "select * from menu where keyName='"+keyName+"'";
		List data = queryMultiColumn(sql);
		if(data == null || data.isEmpty()) return null;
		Map map = (HashMap)data.get(0);
		MenuForm menu = new MenuForm();
		menu.setKeyName(keyName);
		menu.setTitle(map.get("TITLE").toString());
		menu.setFormType(map.get("FORMTYPE").toString());
		menu.setSortNumber(Integer.parseInt(map.get("SORTNUMBER").toString()));
		menu.setLayer(Integer.parseInt(map.get("LAYER").toString()));
		menu.setIsActive(Integer.parseInt(map.get("ISACTIVE").toString()));
		menu.setIsFirstItem(Integer.parseInt(map.get("ISFIRSTITEM").toString()));
		menu.setUrl((String)map.get("URL"));
		menu.setAccessSno((String)map.get("ACCESSSNO"));
		menu.setAccessName((String)map.get("ACCESSNAME"));
		menu.setTarget(map.get("TARGET").toString());
		menu.setHeadImage((String)map.get("HEADIMAGE"));
		menu.setCreateTime(map.get("CREATETIME").toString());
		menu.setMemo((String)map.get("MEMO"));		
		return menu;
	}	
	
	public List findAll(String sql, String[] fieldName) throws Exception{
		List data = queryAssignColumns(sql, fieldName);
		if(data == null || data.isEmpty()) return null;
		List result = new ArrayList();
		int size = data.size();
		for(int i=0;i<size;i++){
			Map map = (HashMap)data.get(i);
			MenuForm menu = new MenuForm();
			menu.setKeyName(map.get("KEYNAME").toString());
			menu.setTitle(map.get("TITLE").toString());
			menu.setFormType(map.get("FORMTYPE").toString());
			menu.setSortNumber(Integer.parseInt(map.get("SORTNUMBER").toString()));
			menu.setLayer(Integer.parseInt(map.get("LAYER").toString()));
			menu.setIsActive(Integer.parseInt(map.get("ISACTIVE").toString()));
			menu.setIsFirstItem(Integer.parseInt(map.get("ISFIRSTITEM").toString()));
			menu.setUrl((String)map.get("URL"));
			menu.setHeadImage((String)map.get("HEADIMAGE"));
			menu.setAccessSno((String)map.get("ACCESSSNO"));
			menu.setAccessName((String)map.get("ACCESSNAME"));
			menu.setTarget(map.get("TARGET").toString());			
			result.add(menu);
		}		
		return result;
	}
	
	public int setIsFirstItem(int value, int old){
		String sql = "update menu set isFirstItem="+value+"where isFirstItem="+old;
		return update(sql);
	}
	
	public int delete(String keyName){
		String sql = "delete from menu where keyName='"+keyName+"'";
		return update(sql);
	}
	
	public synchronized String create(){
		try {
			log.info("开始初始化表menu...");
			String sql = "SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[menu]');";
			List list = queryMultiColumn(sql);
			if(list == null){
				log.warn("系统检测失败。\n原因:\n  1.数据库非正常运行;\n  2.帐户没权限不足;\n  3.数据库连接异常。");
				return "系统检测失败。\n原因:\n  1.数据库非正常运行;\n  2.帐户没权限不足;\n  3.数据库连接异常。";
			}
			if(list.isEmpty()){
				log.info("表company不存在，开始创建...");
				sql = "/****** 对象:  Table [dbo].[menu]    脚本日期: 06/24/2009 17:40:16 ******/ " +
					  "SET ANSI_NULLS ON;" +
					  "SET QUOTED_IDENTIFIER ON;" +
					  "SET ANSI_PADDING ON;" +
					  "CREATE TABLE [dbo].[company](" +
							  	"[id] [int] IDENTITY(1,1) NOT NULL," +
								"[keyName] [varchar](100) NOT NULL," +
								"[title] [varchar](100) NULL," +
								"[formType] [varchar](15) NULL," +
								"[sortNumber] [int] NULL," +
								"[layer] [int] NULL," +
								"[isActive] [int] NULL," +
								"[isFirstItem] [int] NULL," +
								"[url] [varchar](200) NULL," +
								"[accessSno] [varchar](500) NULL," +
								"[accessName] [varchar](500) NULL," +
								"[target] [varchar](10) NULL," +
								"[headImage] [varchar](100) NULL," +
								"[createTime] [varchar](20) NULL," +
								"[memo] [varchar](200) NULL," +		  			
					  "CONSTRAINT [PK_menu] PRIMARY KEY CLUSTERED" +
					  "(" +
					  			"[id] ASC" +
					  ")WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]" +
					  ") ON [PRIMARY];" +
					  "SET ANSI_PADDING OFF;";
				
				if(this.update(sql) < 0){
					log.warn("表menu初始化失败!");
					return "表menu初始化失败!";
				}
				log.info("表menu创建成功!");
				return "表menu创建成功!";
			}
			else{
				log.info("表menu已存在!");
				return "表menu已存在!";
			}
		} 
		catch (Exception e) {
			log.warn("表menu初始化失败!");
			e.printStackTrace();
			return "表menu初始化失败!" + e.toString();
		}
	}
}